package com.adt.studioms.web.controller.user;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.adt.studioms.StudiomsApplication;
import com.adt.studioms.api.PhotoSizeAPI;
import com.adt.studioms.om.PhotoSize;
import com.asao.baseware.core.security.ITrustedPassport;
import com.asao.baseware.core.util.CommonValidation;
import com.asao.baseware.core.web.controller.EntityCommand;
import com.asao.baseware.core.web.controller.EntityFormController;

public class PhotoSizeController extends EntityFormController<PhotoSizeCommand> {
    PhotoSizeAPI photoSizeAPI;

    class PhotoSizeValidator implements Validator {
        
        public boolean supports(Class clazz) {
            return clazz.equals(PhotoSizeCommand.class);
        }

        public void validate(Object target, Errors errors) {
            PhotoSizeCommand cmd = (PhotoSizeCommand) target;
            
            if (StringUtils.isEmpty(cmd.getCode())) {
                errors.rejectValue("code", "photoSize.codeIsRequired", "Code is required");
            } else if (!CommonValidation.isLengthInRange(cmd.getCode(), 1, 10)) {
                errors.rejectValue("code", "photoSize.codeLengthIsNotInRange", "Code length is not in range (1-10)");
            } else if (cmd.getAction()==EntityCommand.CREATE || cmd.getAction()==EntityCommand.UPDATE) {
                PhotoSize photoSize = photoSizeAPI.getByCode(cmd.getCode());
                if (photoSize!=null && photoSize.getId()!=cmd.getId())
                    errors.rejectValue("code", "photoSize.codeIsExisted", "Code is existed");
            }
            
            if (StringUtils.isEmpty(cmd.getName())) {
                errors.rejectValue("name", "photoSize.nameIsRequired", "Name is required");
            } else if (!CommonValidation.isLengthInRange(cmd.getName(), 1, 40)) {
                errors.rejectValue("name", "photoSize.nameLengthIsNotInRange", "Name length is not in range (1-40)");
            }
        }
    }
    
    public PhotoSizeController() {
        super();
        photoSizeAPI = StudiomsApplication.instance.getApiRegistry().lookupAPI(ITrustedPassport.instance, PhotoSizeAPI.class);
        
        setCommandClass(PhotoSizeCommand.class);
        setValidator(new PhotoSizeValidator());
    }
    
    protected void publishEntity(long id, PhotoSizeCommand command) {
        PhotoSize photoSize = photoSizeAPI.get(id);
        if (photoSize!=null) {
            command.setId(id);
            command.setName(photoSize.getName());
            command.setCode(photoSize.getCode());
        }        
    }

    protected void createEntity(PhotoSizeCommand command, BindException errors) {
        PhotoSize ps = photoSizeAPI.createPhotoSize(command.getCode(), command.getName());
        command.setId(ps.getId());
    }
    
    protected void updateEntity(PhotoSizeCommand command, BindException errors) {
        PhotoSize photoSize = photoSizeAPI.get(command.getId());
        if (photoSize!=null) {
            photoSize.setCode(command.getCode());
            photoSize.setName(command.getName());
            
            photoSizeAPI.update(photoSize);
        }
    }

    protected void deleteEntity(PhotoSizeCommand command, BindException errors) {
        photoSizeAPI.delete(command.getId());
    }
}

class PhotoSizeCommand extends EntityCommand {
    String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
